/*
 * SignatureSliceIndex_gtest.h
 *
 *  Created on: Nov 26, 2012
 *      Author: Federico Orquera
 */

#ifndef SIGNATURESLICEINDEX_GTEST_H_
#define SIGNATURESLICEINDEX_GTEST_H_

#include <string>
#include <gtest/gtest.h>

#include "../logic_layer/SignatureSlicingIndexFile/fileHandler/SignatureSliceFileHandler.h"
#include "../logic_layer/SignatureSlicingIndexFile/SignatureSliceIndex.h"

/**
 * Test Suite de la clase SignatureSlicingIndex
 */
namespace SignatureSlicingIndex {

class SignatureSliceIndexTestFixture: public ::testing::Test {
public:

	std::string m_filename;

	SignatureSliceIndexTestFixture(){
		/*
		 * Acá lo único que hago es obtener el nombre
		 * del TestFixture y del TestCase que se está ejecutando
		 * y con eso armo un "filename" del tipo: testname.testcase.tmp
		 */

		const ::testing::TestInfo* const test_info =
				::testing::UnitTest::GetInstance()->current_test_info();

		std::string filename(test_info->name());
		filename.append(".");
		filename.append(test_info->test_case_name());
		filename.append(".tmp");

		m_filename = filename;
	}

	virtual ~SignatureSliceIndexTestFixture() {
	}

	void SetUp() {
	}

	void TearDown() {
	}

	/**
	 * Devuelve un filename como: testname.testcase.tmp
	 *
	 * @return un filename predecible para usar en el test
	 */
	const char* getFileNameForTest() {
		return m_filename.c_str();
	}

};

/**
 * Test Add and retrieve one phrase in the dictionary
 *
 */

TEST_F( SignatureSliceIndexTestFixture, AddAndRetrievePhrase) {

	SignatureSliceFileHandler handler(this->getFileNameForTest(), true);
	SignatureSliceIndex<128> Index(&handler);

	 PhraseList firstList;

	 firstList.push_back(1);
	 firstList.push_back(3);
	 firstList.push_back(7);
	 firstList.push_back(15);

	 Index.InsertRegister(0, firstList);

	 auto queryResult = Index.SearchPhrases(0);

	 ASSERT_EQ(queryResult, firstList);

}

/**
 * Test Add and Modify one phrase in the dictionary
 *
 */

TEST_F( SignatureSliceIndexTestFixture, AddAndModifyePhrase) {

	 SignatureSliceFileHandler handler(this->getFileNameForTest(), true);
	 SignatureSliceIndex<128>  Index(&handler);

	 PhraseList firstList;

	 firstList.push_back(1);
	 firstList.push_back(3);
	 firstList.push_back(7);
	 firstList.push_back(15);

	 Index.InsertRegister(0, firstList);

	 firstList.push_back(16);

	 Index.UpdateRegister(16,0,true);

	 auto queryResult = Index.SearchPhrases(0);

	 ASSERT_EQ(queryResult, firstList);

}

/**
 * Test Add and Modify one phrase in the dictionary
 *
 */

TEST_F( SignatureSliceIndexTestFixture, AddAndDeletePhrase) {

	 SignatureSliceFileHandler handler(this->getFileNameForTest(), true);
	 SignatureSliceIndex<128>  Index(&handler);

	 PhraseList firstList;

	 firstList.push_back(1);
	 firstList.push_back(3);
	 firstList.push_back(7);
	 firstList.push_back(15);

	 Index.InsertRegister(0, firstList);

	 Index.DeleteRegister(15);

	 firstList.pop_back();

	 auto queryResult = Index.SearchPhrases(0);

	 ASSERT_EQ(queryResult, firstList);

}

/**
 * Test Add and Search ALL keywords in the dictionary
 *
 */

TEST_F( SignatureSliceIndexTestFixture, AddAndSearchALL) {

	SignatureSliceFileHandler handler(this->getFileNameForTest(), true);
	SignatureSliceIndex<128>  Index(&handler);

	PhraseList firstList;

	firstList.push_back(1);
	firstList.push_back(3);
	firstList.push_back(7);
	firstList.push_back(15);

	PhraseList secondList;

	secondList.push_back(2);
	secondList.push_back(14);
	secondList.push_back(6);
	secondList.push_back(3);

	PhraseList thirdList;

	thirdList.push_back(1);
	thirdList.push_back(16);
	thirdList.push_back(33);
	thirdList.push_back(24);
	thirdList.push_back(2);
	thirdList.push_back(3);

	Index.InsertRegister(0, firstList);
	Index.InsertRegister(1, secondList);
	Index.InsertRegister(2, thirdList);

	KeyWordList query;

	query.push_back(1);
	query.push_back(2);

	auto queryResult = Index.SearchAllPhrases(query);

	PhraseList expected;

	expected.push_back(2);
	expected.push_back(3);

	ASSERT_EQ(queryResult, expected);

}

/**
 * Test Add and Search ANY keywords in the dictionary
 *
 */

TEST_F( SignatureSliceIndexTestFixture, AddAndSearchANY) {

	SignatureSliceFileHandler handler(this->getFileNameForTest(), true);
	SignatureSliceIndex<128>  Index(&handler);

	PhraseList firstList;

	firstList.push_back(1);
	firstList.push_back(3);
	firstList.push_back(7);
	firstList.push_back(15);

	PhraseList secondList;

	secondList.push_back(2);
	secondList.push_back(14);
	secondList.push_back(6);
	secondList.push_back(3);

	PhraseList thirdList;

	thirdList.push_back(1);
	thirdList.push_back(16);
	thirdList.push_back(33);
	thirdList.push_back(24);
	thirdList.push_back(2);
	thirdList.push_back(3);

	Index.InsertRegister(0, firstList);
	Index.InsertRegister(1, secondList);
	Index.InsertRegister(2, thirdList);

	KeyWordList query;

	query.push_back(1);
	query.push_back(0);

	auto queryResult = Index.SearchAnyPhrase(query);

	PhraseList expected;

	expected.push_back(1);
	expected.push_back(2);
	expected.push_back(3);
	expected.push_back(6);
	expected.push_back(7);
	expected.push_back(14);
	expected.push_back(15);

	ASSERT_EQ(queryResult, expected);

}

}

#endif /* SIGNATURESLICEINDEX_GTEST_H_ */
